// pages/mineService/mineService.js
const app = getApp();
var network = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
      indexs:0,
      issel:false,
      selData: [],
      status:'',
      mineTaskList:[],
      mineReciveList:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
    app.getOpenId().then(function(){
        wx.getSetting({
          success: function (res) {
            if (res.authSetting['scope.userInfo']) {
              wx.getUserInfo({
                success: function (res) {
                  //保存用户信息    
                  app.saveMineMess(res).then(function(){
                    console.log(app.globalData.consumerid)
                    that.getMineStatus();
                    that.getMineServiceList();
                  });
                 
                }
              })
            }
            else {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            }
          }
        })
      })
  },

  //获取我的发布列表
  getMineServiceList: function () {
    var that = this; 
    network.requestLoading('POST', app.globalData.requestUrl + 'getDemandInfoByConsumerIdAPI',
      {
        consumerId: app.globalData.consumerid,
        status: that.data.status
      }, '加载中', function (res) {
        let data = res.data
        for(let i in data){
          if (data[i].statusName == "待完成"){
            data[i].class=''
          }
          if (data[i].statusName == "未接单") {
            data[i].class = ''
          }
          if (data[i].statusName == "已接单") {
            data[i].class = 'rec'
          }
          if (data[i].statusName == "已完成") {
            data[i].class = 'complete'
          }
          if (data[i].statusName == "已结束") {
            data[i].class = 'end'
          }
        }
         that.setData({
           mineTaskList:data
         })
      })
  },
  //获取我的接单列表
  getMineReciveList: function () {
    var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getOrdersInfoByConsumerIdAPI',
      {
        consumerId: app.globalData.consumerid,
        status: that.data.status
      }, '加载中', function (res) {
        console.log(res.data)
        that.setData({
          mineReciveList: res.data
        })
      })
  },
  //获取我的发布需求状态
  getMineStatus: function () {
  var that = this;
    network.requestLoading('POST', app.globalData.requestUrl + 'getOrdersStatusAPI',
    {
      consumerId: app.globalData.consumerid,
      status: that.data.status
    }, '', function (res) {
      that.setData({
        selData: res.data,
      })
    })
},
  radioChange:function(e){
      this.setData({
        status: e.detail.value
      })
  },
  //确定
  submit:function(){
    this.setData({issel: false})
    this.getMineServiceList();
  },
  closeSel:function(){
    this.setData({ issel:false})
  },
  openSel: function () {
    this.setData({ issel: true, status:''})
  },
  swiperChange: function (e) {
    this.setData({
      indexs: e.detail.current
    })
  },
  setIndex:function(e){
     var that =this;
     this.setData({
       indexs: e.currentTarget.dataset.index
     })
    //查询我的接单
    if (e.currentTarget.dataset.index == 1){
      that.getMineReciveList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     
  },
  toDetail:function(e){
    let data = e.currentTarget.dataset.obj
    data =JSON.stringify(data)
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?obj=' + data,
    })
  },
  todemand:function(){
    wx.navigateTo({
      url: "/pages/sendDemand/sendDemand",
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})